package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import com.itheima.utils.DateUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/report")
@RestController
public class ReportController {

    @Reference
    MemberService memberService;

    @Reference
    SetmealService setmealService;

    @Reference
    ReportService reportService;



    @RequestMapping("/getMemberReportAdd")
    public Result getMemberReportAdd(@RequestBody Map timemap){
        String startTime = (String) timemap.get("startTime");
        String endTime = (String) timemap.get("endTime");
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = DateUtils.parseString2Date(startTime);
            endDate = DateUtils.parseString2Date(endTime);
        }catch (Exception e) {
            e.printStackTrace();
        }

        Map<String,Object> map = memberService.getMemberReportAdd(startDate,endDate);
        return new Result(true,MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }


    /**
     * {
     *     months: []
     *      memberCount:[]
     * }
     *
     *
     * @return
     */
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            Map<String,Object> map = memberService.getMemberReport();
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map );
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL );
        }
    }






    //获取不同性别会员的数量
    @RequestMapping("/getMemberSexReport")
    public Result getMemberSexReport() {
        try {
            Map<String, Object> map = memberService.getMemberSexReport();
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    //获取不同年龄段会员的数量
    @RequestMapping("/getMemberAgeReport")
    public Result getMemberAgeReport(){
        try{
            Map<String,Object> map = memberService.getMemberAgeReport();
            return new Result(true,MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport() {
        try {
            Map<String, Object> map = setmealService.getSetmealReport();
            return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_SETMEAL_COUNT_REPORT_FAIL);
        }

    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        try {
            Map<String, Object> map = reportService.getBusinessReportData();
            return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response) {
        try {
            //获取运营统计数据
            Map<String, Object> map = reportService.getBusinessReportData();
            //构建模板流对象
            InputStream inputStream = this.getClass().getResourceAsStream("/template/report_template.xlsx");
            //创建Excel对象
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
            //获取工作表对象
            XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
            //设置统计日期
            //获取第三行
            XSSFRow row = sheet.getRow(2);
            //获取第5个单元格
            XSSFCell cell = row.getCell(5);
            cell.setCellValue(String.valueOf(map.get("reportDate")));
            //新增会员数
            //获取第五行
            row = sheet.getRow(4);
            //获取第6个单元格
            cell = row.getCell(5);
            cell.setCellValue(String.valueOf(map.get("todayNewMember")));
            //总会员数
            //获取第五行
            row = sheet.getRow(4);
            //获取第8个单元格
            cell = row.getCell(7);
            cell.setCellValue(String.valueOf(map.get("totalMember")));
            //本周新增会员数
            //获取第五行
            row = sheet.getRow(5);
            //获取第6个单元格
            cell = row.getCell(5);
            cell.setCellValue(String.valueOf(map.get("thisWeekNewMember")));
            //本月新增会员数
            //获取第五行
            row = sheet.getRow(5);
            //获取第8个单元格
            cell = row.getCell(7);
            cell.setCellValue(String.valueOf(map.get("thisMonthNewMember")));
            //今日预约数
            //获取第8行
            row = sheet.getRow(7);
            //获取第6个单元格
            cell = row.getCell(5);
            cell.setCellValue(String.valueOf(map.get("todayOrderNumber")));
            //今日到诊数
            //获取第8行
            row = sheet.getRow(7);
            //获取第8个单元格
            cell = row.getCell(7);
            cell.setCellValue(String.valueOf(map.get("todayVisitsNumber")));
            //本周预约数
            //获取第9行
            row = sheet.getRow(8);
            //获取第6个单元格
            cell = row.getCell(5);
            cell.setCellValue(String.valueOf(map.get("thisWeekOrderNumber")));
            //本周到诊数
            //获取第9行
            row = sheet.getRow(8);
            //获取第6个单元格
            cell = row.getCell(7);
            cell.setCellValue(String.valueOf(map.get("thisWeekVisitsNumber")));
            //本月预约数
            //获取第9行
            row = sheet.getRow(9);
            //获取第6个单元格
            cell = row.getCell(5);
            cell.setCellValue(String.valueOf(map.get("thisMonthOrderNumber")));
            //本月到诊数
            //获取第9行
            row = sheet.getRow(9);
            //获取第6个单元格
            cell = row.getCell(7);
            cell.setCellValue(String.valueOf(map.get("thisMonthVisitsNumber")));

            List<Map<String, Object>> hotSetmealList = (List<Map<String, Object>>) map.get("hotSetmeal");
            int rowNum = 12;
            for (Map<String, Object> hotSealmeal : hotSetmealList) {
                row = sheet.getRow(rowNum);
                //获取单元格
                cell = row.getCell(4);
                cell.setCellValue(String.valueOf(hotSealmeal.get("name")));
                //获取单元格
                cell = row.getCell(5);
                cell.setCellValue(String.valueOf(hotSealmeal.get("setmeal_count")));
                //获取单元格
                cell = row.getCell(6);
                cell.setCellValue(String.valueOf(hotSealmeal.get("proportion")));

                rowNum++;
            }

            // 通过输出流进行文件下载
            ServletOutputStream out = response.getOutputStream();
            //设置输入的文件的类型
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename="+"_report.xlsx");
            xssfWorkbook.write(out);
            out.flush();
            out.close();
            xssfWorkbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
